package com.db4o.odbgen.plugins.java.maingenerator.typedxmlschema;

import com.db4o.internal.odbgen.StringUtils;
import com.db4o.odbgen.plugins.xmlschema.Element;

/**
 * 
 * Each enum constant may be associated with one or more values.
 * Each value will be mapped later to one {@link TSEnumParameter}.
 * @author liviug
 */
public class TSEnumValue  implements XmlElement, Comparable<TSEnumValue>{
    private Element _element;
    private String _content;
    private TSEnumParameter _associatedParameter;

    /**
     * Constructs an enum value given it's fields.
     * @param element The corresponding element within XmlSchema.
     * @param content The of the value.
     * @param associatedParameter The associated enum parameter.
     */
    public TSEnumValue(
            Element element, 
            String content,
            TSEnumParameter associatedParameter
            ){
        this._element = element;
        this._content = content;
        this._associatedParameter = associatedParameter;
    }
    
    /**
     * See {@link XmlElement} for details.
     */
    @Override
    public Element getElement() {
        return this._element;
    }
    
    /**
     * Returns the content of the value.
     */
    public String getContent(){
        return this._content;
    }
    
    /**
     * Returns the parameter associated with this value.
     */
    public TSEnumParameter getAssociatedParameter(){
        return this._associatedParameter;
    }
    
    @Override
    public int compareTo(TSEnumValue o) {
        return this.getAssociatedParameter().getOrder() - o.getAssociatedParameter().getOrder();
    }

    @Override
    public boolean equals(Object obj) {
    	if(!(obj instanceof TSEnumValue)){
    		return false;
    	}
    	else{
    		return this.compareTo((TSEnumValue)obj)==0;
    	}
    }

    @Override
    public int hashCode() {
    	assert false : "hashCode is not implemented.";
    	return 0;
    }
    
    /**
     * Returns a string representing this object.
     * This is used for debug purposes.
     * @param tabs Number of tabs to indent.
     */
    String toString(int tabs) {
        StringBuilder res = new StringBuilder();
        
        res.append(String.format("%svalue", StringUtils.tabs(tabs)));
        res.append(StringUtils.newLine());

        res.append(String.format("%scontent=%s",
                StringUtils.tabs(tabs+1),
                this.getContent()
                ));
        res.append(StringUtils.newLine());

        res.append(String.format("%sassociatedParameter=%s(order=%s)",
                StringUtils.tabs(tabs+1),
                this.getAssociatedParameter().getName(),
                this.getAssociatedParameter().getOrder()
                ));
        res.append(StringUtils.newLine());
        
        
        return res.toString();
    }

}
